package com.cjy.user.service.impl;

import com.base.common.enums.AppType;
import com.base.common.enums.LogCategory;
import com.base.common.enums.TerminalType;
import com.base.common.persistent.dao.*;
import com.base.common.persistent.model.*;
import com.base.common.utils.EasyUtil;
import com.base.common.webservice.entity.HZService.UserCstGetUsersByMobilePhoneBean;
import com.base.common.webservice.entity.SYS86Service.UserRevOwnerVerifyBean;
import com.base.common.webservice.util.HZServiceUtil;
import com.base.common.webservice.util.SYS86ServiceUtil;
import com.cjy.user.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * userService的实现类
 * 
 * @author kat.luhongkai
 * 
 */

@Service("userService")
public class UserServiceImpl implements UserService {

	@Resource 
	private EasyUtil easyUtil;

	@Resource
	private EmployeeMapper employeeMapper;

	@Resource
	private TerminalMapper terminalMapper;
	
	@Resource
	private TsmsSendCacheMapper tsmsSendCacheMapper;
	
	@Resource
	private UserMapper userMapper;
	
	@Resource
	private VillageConfigMapper villageConfigMapper;

	@Resource
	private CompoundsMapper compoundsMapper;

    @Resource
    private TAirportUserMapper tAirportUserMapper;

    @Resource
    private TLogMapper tLogMapper;

	/**
	 * 城际云android设备获取验证码，准备注册
	 * 
	 */
	@Override
	@Transactional
	public int androidGetCaptcha(Terminal terminal) {
		int result = 0;
		int captcha = getSixCaptcha(); // 验证码生成
		terminal.setCaptcha(captcha); // 设置用户验证码

		List<Terminal> terminals;

		// 判断安卓设备是否已经注册或被拉入黑名单
		terminals = terminalMapper.queryTerminalByCodesRegisted(null, null,
				terminal.getCode(), AppType.CJY.getValue());
		if (terminals.size() > 0) {
			if (terminals.get(0).getStatus() == 3) {
                List<User> users = this.updateNewUser(terminal);
                if (users.size() > 0) {
                    Terminal terminal1 = new Terminal();
                    terminal1.setId(terminals.get(0).getId());
                    terminal1.setStatus(1);
                    if (terminalMapper.updateTerminalById(terminal1) == 1){
                        result = 5;
                    }
                } else {

                    result = 4;
                }
			} else {
				result = 2;
			}
		} else {

			// 判断手机号是否已经注册
			terminals = terminalMapper.queryTerminalByPhoneRegisted(terminal
					.getPhone().trim(), AppType.CJY.getValue());
			if (terminals.size() > 0) {

				// 判断注册的手机号是否已经绑定设备码
				if (terminals.get(0).getCode() == null
						|| Objects.equals("", terminals.get(0).getCode().trim())) {

					// 绑定设备码
					terminal.setId(terminals.get(0).getId());
					if (terminalMapper.updateTerminalById(terminal) > 0) {
						result = 1;
					} else {
						result = 0;
					}
				} else {
					result = 3;
				}
			} else {
				terminal.setStatus(0); // 设置状态码，默认为0（未通过）

				// 判断该微信是否已经获取过验证码，处于未注册状态
				terminals = terminalMapper
						.queryTerminalByCodesOrPhoneNotRegisted(null,
								null, terminal.getCode(), null,
								AppType.CJY.getValue());
				if (terminals.size() > 0) {
					// 更新设备信息
					terminal.setId(terminals.get(0).getId());
					result = terminalMapper.updateTerminalById(terminal);
				} else if (terminalMapper
						.queryTerminalByCodesOrPhoneNotRegisted(null, null,
								null, terminal.getPhone(),
								AppType.CJY.getValue()).size() > 0) {

					// 绑定设备码
					if (terminalMapper.updateTerminalById(terminal) > 0) {
						result = 1;
					} else {
						result = 0;
					}
				} else {
					// 新增微信设备
					terminal.setBid(AppType.CJY.getValue());
					result = terminalMapper.addTerminal(terminal);
				}
			}
		}
		if(result == 1){
			easyUtil.sendSms("验证码：" + captcha, terminal.getPhone());
		}
		
		return result;
	}

	/**
	 * 获取六位数的验证码
	 * 
	 */
    private int getSixCaptcha() {
		Random rand = new Random();
        return Math.abs(rand.nextInt()) % (9999 - 1000 + 1) + 1000;
	}

	/**
	 * 城际云ios设备获取验证码，准备注册
	 * 
	 */
	@Override
	@Transactional
	public int iosGetCaptcha(Terminal terminal) {
		int result = 0;
		int captcha = getSixCaptcha(); // 验证码生成
		terminal.setCaptcha(captcha); // 设置用户验证码

		List<Terminal> terminals;

		// 判断ios设备是否已经注册或被拉入黑名单
		terminals = terminalMapper.queryTerminalByCodesRegisted(null, terminal
				.getAppleId().trim(), null, AppType.CJY.getValue());
		if (terminals.size() > 0) {
            if (terminals.get(0).getStatus() == 3) {
                List<User> users = this.updateNewUser(terminal);
                if (users.size() > 0) {
                    Terminal terminal1 = new Terminal();
                    terminal1.setId(terminals.get(0).getId());
                    terminal1.setStatus(1);
                    if (terminalMapper.updateTerminalById(terminal1) == 1){
                        result = 5;
                    }
                } else {
                    result = 4;
                }
            } else {
                result = 2;
            }
		} else {

			// 判断手机号是否已经注册
			terminals = terminalMapper.queryTerminalByPhoneRegisted(terminal
					.getPhone().trim(), AppType.CJY.getValue());
			if (terminals.size() > 0) {

				// 判断注册的手机号是否已经绑定设备码
				if (terminals.get(0).getAppleId() == null
						|| Objects.equals("", terminals.get(0).getAppleId().trim())) {

					// 绑定设备码
					terminal.setId(terminals.get(0).getId());
					if (terminalMapper.updateTerminalById(terminal) > 0) {
						result = 1;
					} else {
						result = 0;
					}
				} else {
					result = 3;
				}
			} else {
				terminal.setStatus(0); // 设置状态码，默认为0（未通过）

				// 判断该微信是否已经获取过验证码，处于未注册状态
				terminals = terminalMapper
						.queryTerminalByCodesOrPhoneNotRegisted(null, terminal.getAppleId(), null, null, AppType.CJY
								.getValue());
				if (terminals.size() > 0) {
					// 更新设备信息
					terminal.setId(terminals.get(0).getId());
					result = terminalMapper.updateTerminalById(terminal);
				} else if (terminalMapper
						.queryTerminalByCodesOrPhoneNotRegisted(null, null,
								null, terminal.getPhone().trim(),
								AppType.CJY.getValue()).size() > 0) {

					// 绑定设备码
					if (terminalMapper.updateTerminalById(terminal) > 0) {
						result = 1;
					} else {
						result = 0;
					}
				} else {
					// 新增微信设备
					terminal.setBid(AppType.CJY.getValue());
					result = terminalMapper.addTerminal(terminal);
				}
			}
		}
		if(result == 1){
			easyUtil.sendSms("验证码：" + captcha, terminal.getPhone());
		}
		
		return result;
	}

	@Override
	public List login(Terminal terminal) {
		List list = new ArrayList();
		try {

			List<Terminal> terminals = new ArrayList<>();

			// 判断登录设备
			if (terminal.getBid() == TerminalType.CJY_ANDROID.getValue() || terminal.getBid() == TerminalType.CJY_IOS.getValue()
					|| terminal.getBid() == TerminalType.JCJY_ANDROID.getValue() || terminal.getBid() == TerminalType.JCJY_IOS.getValue()) {
				
				// 设置手机号
				if(terminal.getBid() == TerminalType.CJY_ANDROID.getValue() || terminal.getBid() == TerminalType.JCJY_ANDROID.getValue()){
					 terminals = terminalMapper.queryTerminalByCodesRegisted(null, null, terminal.getCode(), AppType.CJY.getValue());
				} else {
					 terminals = terminalMapper.queryTerminalByCodesRegisted(null, terminal.getAppleId(), null, AppType.CJY.getValue());
				}

				if (terminals.size() == 1) {
                    TLog tLog = new TLog();
                    tLog.setAppType(terminal.getBid());
                    tLog.setContent("用户：" + terminals.get(0).getPhone() + "执行登录操作");
                    tLog.setTerminalId((long) terminals.get(0).getId());
                    tLog.setCategory(LogCategory.LOGIN.getValue());
                    tLogMapper.insertSelective(tLog);
					terminal.setBid(AppType.CJY.getValue());
					list = userMapper.login(terminal);
				}
				/*String c = villageConfigMapper.getProjectID(terminals.get(0).getUserChooseCompoundsId());
                List<Compounds> compoundses = compoundsMapper.queryCompoundsById(terminals.get(0).getUserChooseCompoundsId());
                if (compoundses.get(0).getCompany().getId() == 5) {
                    List<UserCstGetUsersByMobilePhoneBean> userCstGetUsersByMobilePhoneBeens = HZServiceUtil.getUserCstGetUsersByMobilePhone(terminals.get(0).getPhone());
                    for (int i = 0; i < userCstGetUsersByMobilePhoneBeens.size(); i++) {
                        for(Object o : list){
                            User user = (User) o;
                        }
                    }

                } else {
                    List<UserRevOwnerVerifyBean> revOwnerVerifyBeans = SYS86ServiceUtil.getUserRev_OwnerVerify(null, terminals.get(0).getPhone(), c);
                    for (int i = 0; i < revOwnerVerifyBeans.size(); i++) {
                        for(Object o : list){
                            User user = (User) o;
                        }
                    }

                }*/
                /*for (Object user : list) {
                    ((User) user).setPhone(terminals.get(0).getPhone());
                }*/


			}

		} catch (Exception e) {
			e.printStackTrace();
			return list;
		}
		return list;
	}


	/**
	 * @param terminal
	 * @return
	 */
	/*
	 * @Transactional public List userLogin(Terminal terminal, List list) {
	 * if(terminal.getBid() == 2) {
	 * 
	 * } else { } for(Object user : list) {
	 * ((User)user).setPhone(terminal.getPhone()); } return list; }
	 *//**
	 * 员工登录
	 * 
	 */
	/*
	 * @Transactional public List employeeLogin(Terminal terminal, List list) {
	 * 
	 * for(Object employee : list) { ((Employee)
	 * employee).setPhone(terminal.getPhone()); } return list; }
	 */

	@Override
	public int managerRegiste(Terminal terminal) {

		// 判断ios设备是否已经注册或被拉入黑名单
		int result;
		List<Terminal> terminals;

		if (terminal.getBid() == 1) {
			terminals = terminalMapper.queryTerminalByCodesRegisted(null, null,
					terminal.getCode(), AppType.YGJ.getValue());
		} else {
			terminals = terminalMapper.queryTerminalByCodesRegisted(null,
					terminal.getAppleId(), null, AppType.YGJ.getValue());
		}

		if (terminals.size() > 0) {
			result = 2;
		} else {

			// 判断手机号是否已经注册
			terminals = terminalMapper.queryTerminalByPhoneRegisted(terminal
					.getPhone().trim(), AppType.YGJ.getValue());
			if (terminals.size() > 0) {

				// 判断注册的手机号是否已经绑定设备码
				if (terminal.getBid() == 1) {
					if (terminals.get(0).getCode() == null
							|| Objects.equals("", terminals.get(0).getCode().trim())) {

						// 绑定设备码
						terminal.setId(terminals.get(0).getId());
						if (terminalMapper.updateTerminalById(terminal) > 0) {
							result = 1;
						} else {
							result = 0;
						}
					} else {
						result = 3;
					}
				} else {
					if (terminals.get(0).getAppleId() == null
							|| !Objects.equals("", terminals.get(0).getAppleId().trim())) {

						// 绑定设备码
						terminal.setId(terminals.get(0).getId());
						if (terminalMapper.updateTerminalById(terminal) > 0) {
							result = 1;
						} else {
							result = 0;
						}
					} else {
						result = 3;
					}
				}

			} else {
				terminal.setStatus(1); // 设置状态码，默认为0（未通过）

					// 查询当前手机好是否在员工档案中已经存在，存在则在中间表中添加对应记录
					List<Employee> employees = employeeMapper
							.queryEmployeeByPhone(terminal.getPhone().trim());

					if (employees.size() > 0) {
						int bid = terminal.getBid();
						terminal.setBid(AppType.YGJ.getValue());
						if(terminalMapper.addTerminal(terminal) <= 0){
							try {
								throw new Exception();
							} catch (Exception e) {
//								e.printStackTrace();
								return 0;
							}
						}
						
						if (bid == 1) {
							terminals = terminalMapper.queryTerminalByCodesRegisted(null, null,
									terminal.getCode(), AppType.YGJ.getValue());
						} else {
							terminals = terminalMapper.queryTerminalByCodesRegisted(null,
									terminal.getAppleId().trim(), null, AppType.YGJ.getValue());
						}

						// 员工档案中存在该手机号，设置更新员工档案的设备外键
						if (employeeMapper.updateEmployeeTerminalInfoById(
										employees.get(0).getId(), terminals
												.get(0).getId()) > 0) {
							result = 1;
						} else {
							result = 0;
						}

//					}
					/*
					 * } else { // 新增微信设备 result =
					 * terminalMapper.addTerminal(terminal); }
					 */
				} else {
					return 0;
				}

			}

		}
		return result;
	}

	@Transactional
	@Override
	public int registe(Terminal terminal) {
		int result = 1;
		if ((terminal.getPhone() != null && !Objects.equals("", terminal.getPhone().trim()))
				&& ((terminal.getCode() != null && !Objects.equals("", terminal.getCode().trim())) || (terminal
						.getOpenId() != null && !Objects.equals("", terminal.getOpenId().trim())) || (terminal.getAppleId() != null && !Objects.equals("", terminal.getAppleId().trim())))) {


			List<Terminal> terminals;
			
			if(terminal.getBid() == 0){
				terminals = terminalMapper.queryTerminalByCodesAndPhoneNotRegisted(null, null, terminal.getCode(), terminal.getPhone(), AppType.CJY.getValue());
			} else if(terminal.getBid() == 2) {
				terminals = terminalMapper.queryTerminalByCodesAndPhoneNotRegisted(terminal.getOpenId(), null, null, terminal.getPhone(), AppType.CJY.getValue());
			} else {
				terminals = terminalMapper.queryTerminalByCodesAndPhoneNotRegisted(null, terminal.getAppleId(), null, terminal.getPhone(), AppType.CJY.getValue());
			}
			

			// 判断验证码是否正确
			if (terminal.getCaptcha() != terminals.get(0).getCaptcha()) {
				return 2;
			}

			// 查询当前手机好是否在用户档案中已经存在，存在则在中间表中添加对应记录
			List<User> users = userMapper.queryUserByPhone(terminal.getPhone()
					.trim());
			
//			System.out.println(terminals.get(0).getUserChooseCompoundsId());

			terminal.setId(terminals.get(0).getId());
			if (users.size() > 0) {

				// 用户档案中存在该手机号，设置设备信息状态为有效状态
				terminal.setStatus(1);
				// 判断是微信注册还是app注册
				terminalMapper.updateTerminalById(terminal);
				for (User user : users) {
					userMapper.addUserInfo(user.getId(), terminals.get(0)
							.getId());
				}
			} else {



				// 访问思源接口，进行用户认证

                List<Compounds> compoundses = compoundsMapper.queryCompoundsById(terminals.get(0).getUserChooseCompoundsId());
                if (compoundses.get(0).getCompany().getId() == 5) {

                    // 汇众
                    List<UserCstGetUsersByMobilePhoneBean> userCstGetUsersByMobilePhoneBeans = HZServiceUtil.getUserCstGetUsersByMobilePhone(terminal.getPhone());
                    if (userCstGetUsersByMobilePhoneBeans.size() > 0) {
                        userCstGetUsersByMobilePhoneBeans.forEach((item) -> {
                            List<User> userList = userMapper.queryUserByCodeAndResID(item.getResID(), item.getCstID());
                            if(userList.size()  == 0){
                                int comID = villageConfigMapper.getCompoundsID(item.getOrgID());
                                User user = new User();
                                user.setBuildingNo(item.getBudName());
                                user.setResID(item.getResID().trim());
                                user.setCompounds(new Compounds().setId(comID));
                                user.setIDCardNo(item.getRicNo());
                                user.setRoomNo(item.getResCode());
                                user.setPhone(terminal.getPhone().trim());
                                user.setName(item.getCstName());
                                user.setType(1);
                                user.setCode(item.getCstID().trim());

                                user.setCategories(1);
                                user.setIDCardType(0);
                                userMapper.addUser(user);
                                int id = userMapper.queryUserByCodeAndResID(item.getResID(), item.getCstID()).get(0).getId();

                                userMapper.addUserInfo(id, terminals.get(0)
                                        .getId());
                            } else if(userList.size()  == 1) {
                                userMapper.addUserInfo(userList.get(0).getId(), terminals.get(0)
                                        .getId());
                            }
                            terminal.setStatus(1);
                            System.out.println(terminalMapper.updateTerminalById(terminal));
                        });
                    	return 1;
                    } else {
						// 用户档案中不存在该手机记录，更新设备信息为申请状态
						terminal.setStatus(3);
						System.out.println(terminalMapper.updateTerminalById(terminal));
						return 3;
					}
                } else {

                    // 城铁
                    String c = villageConfigMapper.getProjectID(terminals.get(0).getUserChooseCompoundsId());
                    List<UserRevOwnerVerifyBean> revOwnerVerifyBeans = SYS86ServiceUtil.getUserRev_OwnerVerify(null, terminal.getPhone(), c);
                    if (revOwnerVerifyBeans.size() > 0) {
                        for(UserRevOwnerVerifyBean revOwnerVerifyBean : revOwnerVerifyBeans) {
                            List<User> userList = userMapper.queryUserByCodeAndResID(revOwnerVerifyBean.getResID(), revOwnerVerifyBean.getCusID());
                            if (userList.size() == 0) {
                                User user = new User();
                                user.setBuildingNo(revOwnerVerifyBean.getBuildingName());
                                user.setResID(revOwnerVerifyBean.getResID().trim());
                                user.setCompounds(new Compounds().setId(terminals.get(0).getUserChooseCompoundsId()));
                                user.setIDCardNo(revOwnerVerifyBean.getIDCardNo());
                                user.setRoomNo(revOwnerVerifyBean.getResCode());
                                user.setPhone(terminal.getPhone().trim());
                                user.setName(revOwnerVerifyBean.getCusName());
                                user.setType(1);
                                user.setCode(revOwnerVerifyBean.getCusID().trim());

                                String categories = revOwnerVerifyBean.getCusProperty().trim();

                                if("业主".equals(categories)) {
                                    user.setCategories(1);
                                } else if("租户".equals(categories)) {
                                    user.setCategories(2);
                                }

                                String IDCardName = revOwnerVerifyBean.getIDCardName();

                                if("身份证".equals(IDCardName)) {
                                    user.setIDCardType(0);
                                }

                                userMapper.addUser(user);

                                userList = userMapper.queryUserByCodeAndResID(revOwnerVerifyBean.getResID(), revOwnerVerifyBean.getCusID());
                                int id = userList.get(0).getId();
                                userMapper.addUserInfo(id, terminals.get(0)
                                        .getId());
                            } else if(revOwnerVerifyBeans.size() == 1) {
                                userMapper.addUserInfo(userList.get(0).getId(), terminals.get(0)
                                        .getId());
                            }

                            terminal.setStatus(1);
                            System.out.println(terminalMapper.updateTerminalById(terminal));
                        }
                        return 1;
                    } else {
                        // 用户档案中不存在该手机记录，更新设备信息为申请状态
                        terminal.setStatus(3);
                        System.out.println(terminalMapper.updateTerminalById(terminal));
                        return 3;
                    }
                }
			}
		} else return 0;
		return result;
	}

	/**
	 * 微信设备获取验证码，准备注册
	 *
	 */
	@Override
	@Transactional
	public int weChatGetCaptcha(Terminal terminal) {

		int result;
		int captcha = getSixCaptcha(); // 验证码生成
		terminal.setCaptcha(captcha); // 设置用户验证码

		List<Terminal> terminals;

		// 判断微信设备是否已经注册或被拉入黑名单
		terminals = terminalMapper.queryTerminalByCodesRegisted(terminal
				.getOpenId().trim(), null, null, AppType.CJY.getValue());
		if (terminals.size() > 0) {
			result = 2;
		} else {
			// 判断手机号是否已经注册
			terminals = terminalMapper.queryTerminalByPhoneRegisted(terminal
					.getPhone().trim(), AppType.CJY.getValue());
			if (terminals.size() > 0) {

				// 判断注册的手机号是否已经绑定微信号
				if (terminals.get(0).getOpenId() == null
						|| Objects.equals("", terminals.get(0).getOpenId().trim())) {
					// 绑定微信号
					terminal.setId(terminals.get(0).getId());
					if (terminalMapper.updateTerminalById(terminal) > 0) {
						result = 1;
					} else {
						result = 0;
					}
				} else {
					result = 3;
				}
			} else {
				terminal.setStatus(0); // 设置状态码，默认为0（未通过）

				// 判断该微信是否已经获取过验证码，处于未注册状态
				terminals = terminalMapper
						.queryTerminalByCodesOrPhoneNotRegisted(
								terminal.getOpenId(), null, null, null,
								AppType.CJY.getValue());
				if (terminals.size() > 0) {
					// 更新设备信息
					terminal.setId(terminals.get(0).getId());
					return terminalMapper.updateTerminalById(terminal);
				} else if (terminalMapper
						.queryTerminalByCodesOrPhoneNotRegisted(null, null,
								null, terminal.getPhone(),
								AppType.CJY.getValue()).size() > 0) {
					result = 3;
				} else {
					// 新增微信设备
					result = terminalMapper.addTerminal(terminal);
				}
			}
		}
		if(result == 1){
			easyUtil.sendSms("验证码：" + captcha, terminal.getPhone());
		}
		return result;
	}

	@Override
	public List<User> updateNewUser(Terminal terminal) {
        //String[] resIdArr = resIds.split(",");
        List<Terminal> terminals;
        if(terminal.getBid() == 0){
            terminals = terminalMapper.queryTerminalByCodesRegisted(null, null, terminal.getCode(), AppType.CJY.getValue());
        } else if (terminal.getBid() == 2) {
            terminals = terminalMapper.queryTerminalByCodesRegisted(terminal.getOpenId(), null, null, AppType.CJY.getValue());
        } else {
            terminals = terminalMapper.queryTerminalByCodesRegisted(null, terminal.getAppleId(), null, AppType.CJY.getValue());
        }

        terminal.setBid(AppType.CJY.getValue());
        terminal.setPhone(terminals.get(0).getPhone());
        List<User> userListOld = userMapper.login(terminal);
        /*List<String> ResIDs = users.stream().map(User::getResID).collect(Collectors.toList());
        List<String> CusIDs = users.stream().map(User::getCode).collect(Collectors.toList());*/

        // 汇众思源
        List<UserCstGetUsersByMobilePhoneBean> userCstGetUsersByMobilePhoneBeans = HZServiceUtil.getUserCstGetUsersByMobilePhone(terminal.getPhone());
        for (UserCstGetUsersByMobilePhoneBean item : userCstGetUsersByMobilePhoneBeans) {
//            List<String> collect = Arrays.stream(resIdArr).collect(Collectors.toList());
			/*int flag = 1;
			for (User user : users) {
				if (user.getCode().trim().equals(item.getCstID().trim()) && user.getResID().trim().equals(item.getResID().trim())) {
					flag = 0;
					break;
				}
			}
			*//*int index1 = ResIDs.indexOf(item.getResID().trim());
            int index2 = CusIDs.indexOf(item.getCstID().trim());*//*
            if (flag == 1) {*/
            List<User> users = userMapper.queryUserByCodeAndResID(item.getResID(), item.getCstID());
            if (users.size() == 0) {
                int comID = villageConfigMapper.getCompoundsID(item.getOrgID());
                User user = new User();
                user.setBuildingNo(item.getBudName());
                user.setResID(item.getResID());
                user.setCompounds(new Compounds().setId(comID));
                user.setIDCardNo(item.getRicNo());
                user.setRoomNo(item.getResCode());
                user.setPhone(terminal.getPhone().trim());
                user.setName(item.getCstName());
                user.setType(1);
                user.setCode(item.getCstID());

                user.setCategories(1);
                user.setIDCardType(0);
                userMapper.addUser(user);

                int id = userMapper.queryUserByCodeAndResID(item.getResID(), item.getCstID()).get(0).getId();
                userMapper.addUserInfo(id, terminals.get(0)
                        .getId());

            }  else if (users.size() == 1) {
				int flag = 0;
                for (User anUserListOld : userListOld) {
                    if (anUserListOld.getId() == users.get(0).getId()) {
                        flag = 1;
                        break;
                    }
                }
				if (flag == 0){
					userMapper.addUserInfo(users.get(0).getId(), terminals.get(0)
							.getId());
				}
            }
        }


        // 城铁思源
        List<UserRevOwnerVerifyBean> revOwnerVerifyBeans = SYS86ServiceUtil.getUserRev_OwnerVerify(null, terminal.getPhone(), null);
        // 2017-5-24 查询当前用户所有资源并比对
        if (revOwnerVerifyBeans.size() > 0) {
            for(UserRevOwnerVerifyBean revOwnerVerifyBean : revOwnerVerifyBeans) {
//                List<String> collect = Arrays.stream(resIdArr).collect(Collectors.toList());
//                int index1 = ResIDs.indexOf(revOwnerVerifyBean.getResID().trim());;
//                int index2 = CusIDs.indexOf(revOwnerVerifyBean.getCusID().trim());

                List<User> users = userMapper.queryUserByCodeAndResID(revOwnerVerifyBean.getResID(), revOwnerVerifyBean.getCusID());
                if (users.size() == 0) {
                    User user = new User();
                    user.setBuildingNo(revOwnerVerifyBean.getBuildingName());
                    user.setResID(revOwnerVerifyBean.getResID());
                    user.setCompounds(new Compounds().setId(terminals.get(0).getUserChooseCompoundsId()));
                    user.setIDCardNo(revOwnerVerifyBean.getIDCardNo());
                    user.setRoomNo(revOwnerVerifyBean.getResCode());
                    user.setPhone(terminal.getPhone().trim());
                    user.setName(revOwnerVerifyBean.getCusName());
                    user.setType(1);
                    user.setCode(revOwnerVerifyBean.getCusID());

                    String categories = revOwnerVerifyBean.getCusProperty().trim();

                    if("业主".equals(categories)) {
                        user.setCategories(1);
                    } else if("租户".equals(categories)) {
                        user.setCategories(2);
                    }

                    String IDCardName = revOwnerVerifyBean.getIDCardName();

                    if("身份证".equals(IDCardName)) {
                        user.setIDCardType(0);
                    }

                    int id = userMapper.queryUserByCodeAndResID(revOwnerVerifyBean.getResID(), revOwnerVerifyBean.getCusID()).get(0).getId();

                    userMapper.addUserInfo(id, terminals.get(0)
                            .getId());
                } else if (users.size() == 1) {
					int flag = 0;
                    for (User anUserListOld : userListOld) {
                        if (anUserListOld.getId() == users.get(0).getId()) {
                            flag = 1;
                            break;
                        }
                    }
					if (flag == 0){
						userMapper.addUserInfo(users.get(0).getId(), terminals.get(0)
								.getId());
					}

                }

                    /*List<User> users = userMapper.queryUserByCodeAndResID(revOwnerVerifyBean.getResID(), revOwnerVerifyBean.getCusID());
                    int id = users.get(0).getId();

                    userMapper.addUserInfo(id, terminals.get(0)
                            .getId());*/

            }
        }
        List<User> resultList = new ArrayList<>();
        List<User> userListNew = userMapper.login(terminal);
        for (User anUserListNew : userListNew) {
            boolean flag = true;
            for (User user1 : userListOld) {
                if (anUserListNew.getId() == user1.getId()) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                resultList.add(anUserListNew);
            }
            /*List<String> resIDList = userListOld.stream().map(User::getResID).collect(Collectors.toList());
            List<String> cusIDList = userListOld.stream().map(User::getCode).collect(Collectors.toList());
            int i1 = resIDList.indexOf(user.getResID());
            int i2 = cusIDList.indexOf(user.getCode());
            if (i1 == -1 && i2 == -1) {
                resultList.add(user);
            }*/
        }

        return resultList;
	}

	@Override
	public Object airportRegister(String phone) {
        String uuid;
		if(!"".equals(phone)) {
            TAirportUserExample airportUserExample = new TAirportUserExample();
            airportUserExample.createCriteria().andPhoneEqualTo(Integer.valueOf(phone.trim()));
            List<TAirportUser> tAirportUsers = tAirportUserMapper.selectByExample(airportUserExample);
            if (tAirportUsers.size() > 0) {
                return tAirportUsers.get(0);
            }
            uuid = UUID.randomUUID().toString();
            TAirportUser tAirportUser = new TAirportUser();
            tAirportUser.setPhone(Integer.valueOf(phone));
            tAirportUser.setUuid(uuid);
            int r1 = tAirportUserMapper.insertSelective(tAirportUser);
            if (r1 == 1) {
                TAirportUserExample tAirportUserExample = new TAirportUserExample();
                tAirportUserExample.createCriteria().andUuidEqualTo(uuid);
                TAirportUser airportUser = tAirportUserMapper.selectByExample(tAirportUserExample).get(0);
                airportUser.setUsername("AI" + "-" + airportUser.getId());
                int r2 = tAirportUserMapper.updateByPrimaryKeySelective(airportUser);
                if (r2 == 1) {
                    return tAirportUserMapper.selectByPrimaryKey(airportUser.getId());
                }
            }
        }
		return null;
	}

    @Override
    public Object validatePhoneNum(String phone) {
        int captcha = getSixCaptcha(); // 验证码生成
        easyUtil.sendSms("验证码：" + captcha, phone);
        return captcha;
    }

}